<!DOCTYPE html>
<html>
<head>
  #include('head.html',{title:'学校管理'})
  <style>
    .layui-btn .layui-icon{
      margin-right: 0;
    }
  </style>
</head>
<body>

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">ID</label>
            <div class="layui-input-block">
              <input type="text" name="id" placeholder="请输入" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">学校名</label>
            <div class="layui-input-block">
              <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-front-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
        </div>
      </div>
      
      <div class="layui-card-body">
        <div style="padding-bottom: 10px;">
          <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
          <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
        </div>
        
        <table id="LAY-school-manage" lay-filter="LAY-school-manage"></table>
        <script type="text/html" id="imgTpl"> 
          <img style="display: inline-block; height: 100%;cursor: pointer;" onclick="javascript:myimages('/images/picture/{{ d.sPic }}')" src="/images/picture/{{ d.sPic }}">
        </script>
        <script type="text/html" id="table-useradmin-webuser">
          <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a class="layui-btn layui-btn-xs" lay-event="pic"><i class="layui-icon layui-icon-picture-fine"></i></a>
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
        </script>
      </div>
    </div>
  </div>

  <script src="/layuiadmin/layui/layui.js"></script>
  <script>
  layui.config({
    base: '/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'useradmin', 'table'], function(){
    var $ = layui.$
    ,form = layui.form
    ,table = layui.table;
    
    //监听搜索
    form.on('submit(LAY-front-search)', function(data){
      var field = data.field;
        field.page = 1;
      
      //执行重载
      table.reload('LAY-school-manage', {
        where: field
      });
    });
  
    //事件
    var active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('LAY-school-manage')
        ,checkData = checkStatus.data, //得到选中的数据
        ids = [];

        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }
        for (var i in checkData) {
            ids.push(checkData[i].sId);
        }
        layer.prompt({
          formType: 1
          ,title: '敏感操作，请验证密码'
        }, function(value, index) {
            if (checkpass(value)) {
                layer.close(index);
                layer.confirm('确定删除吗？', function (index) {
                    $.ajax({
                        url: "delSchool",
                        dataType: "json",
                        type: "post",
                        data: {
                            ids: ids
                        },
                        success: function (data) {
                            if (data.status == 1) {
                                layer.msg('删除成功', {
                                    icon: 1
                                    , time: 1000
                                }, function () {
                                    table.reload('LAY-school-manage');
                                });
                            }
                            else if (data.status == 0) {
                                layer.msg('删除失败', {
                                    icon: 0
                                    , time: 1000
                                });
                            }
                        },
                        error: function (result) {
                            console.log(result);
                            alert("系统繁忙！");
                        }
                    });
                });
            }
        });
      }
      ,add: function(){
        var index = top.layer.open({
          type: 2
          ,title: '添加学校'
          ,content: 'schoolAdd'
          ,maxmin: true
          ,area: ['800px', '700px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = top.window['layui-layer-iframe'+ index]
            ,submitID = 'LAY-front-submit'
            ,submit = layero.find('iframe').contents().find('#'+ submitID);

            //监听提交
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段
              
              //提交 Ajax 成功后，静态更新表格中的数据
              $.ajax({
                    url: "addSchool",
                    dataType: "json",
                    type: "post",
                    data: field,
                    success: function (data) {
                        if (data.status == 1) {
                            top.layer.msg('添加成功', {
                                icon: 1
                                , time: 1000
                            }, function () {
                                top.layer.close(index); //关闭弹层
                                table.reload('LAY-school-manage'); //数据刷新
                            });
                        }
                        else if (data.status == 0) {
                            top.layer.msg('添加失败', {
                                icon: 0
                                , time: 1000
                            });
                        }
                    },
                    error: function (result) {
                        console.log(result);
                        alert("系统繁忙！");
                    }
                });
            });  
            
            submit.trigger('click');
          }
        });
        //如果手机端，全屏显示
        if(window.innerWidth <= 768) {
            top.layui.layer.full(index);
            setTimeout(function () {
                top.layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            }, 500)
        }
      }
    };
    
    $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  </script>
</body>
</html>
